//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Blogs.Components;

namespace CommunityServer.Blogs.Controls
{
	/// <summary>
	/// Summary description for TagBreadCrumb.
	/// </summary>
	public class TagBreadCrumb : CommunityServer.Controls.TagBreadCrumb
	{
		protected override void CreateChildControls() 
		{
			if (this.ShowHome)
			{
				this.ShowHome = false;
				crumbs.Enqueue(GetAnchor(ResourceManager.GetString("home"), BlogUrls.Instance().HomePage(this.CurrentWeblog.ApplicationKey)));
			}

			base.CreateChildControls();
		}

		protected override string GetTagUrl()
		{
			return BlogUrls.Instance().TagsHome(this.CurrentWeblog.ApplicationKey);
		}

		protected override string GetTagUrl(string[] tagsPath)
		{
			return BlogUrls.Instance().TagsBrowser(this.CurrentWeblog.ApplicationKey, tagsPath);
		}

		protected override string GetTagRssUrl(string[] tagsPath)
		{
			if (this.CurrentWeblog.EnableTagsRss)
			{
				CSContext context = CSContext.Current;
				bool isAvailableToPublic = Permissions.ValidatePermissions(CurrentWeblog, Permission.View, Users.GetAnonymousUser(true));

				if (isAvailableToPublic)
					return BlogUrls.Instance().Rss(this.CurrentWeblog.ApplicationKey, tagsPath, true);
				else if (context.SiteSettings.EnableSecureSyndication)
					return SiteUrls.FormatUrlWithUserNameToken(BlogUrls.Instance().Rss(this.CurrentWeblog.ApplicationKey, tagsPath, true), context.User, true);
			}

			return null;
		}

		#region Weblog Properties

		private Weblog weblog = null;

		/// <summary>
		/// Returns the CurrentWeb and calls Authorize
		/// </summary>
		public virtual Weblog CurrentWeblog
		{
			get
			{
				if(weblog == null)
				{
					string appKey = CSContext.Current.ApplicationKey;
					if(appKey == null)
						appKey = WeblogConfig.DefautApplicationKey;

					weblog = Weblogs.GetWeblog(appKey,true);

					if(!this.CurrentUser.IsBlogAdministrator)
						Authorize(weblog);
				}

				return weblog;
			}
		}

		protected User CurrentUser
		{
			get{return csContext.User;}
		} 

		private WeblogConfiguration weblogConfig = null;
		protected WeblogConfiguration WeblogConfig
		{
			get
			{
				if(weblogConfig == null)
					weblogConfig = WeblogConfiguration.Instance();

				return weblogConfig;
			}
		}

		/// <summary>
		/// By default, checks to see if a blog exists and if the user has read access
		/// </summary>
		/// <param name="w"></param>
		protected virtual void Authorize(Weblog w)
		{
			if(w == null)
				PermissionBase.RedirectOrExcpetion(CSExceptionType.SectionNotFound,string.Format("Weblog {0} could not be found",CSContext.Current.ApplicationKey));

			if(Context.Items[CurrentUser.Username + "Permission:" + Permission.View.ToString()] == null)
			{
				Permissions.AccessCheck(w,Permission.View,this.CurrentUser);
				Context.Items[CurrentUser.Username + "Permission:" + Permission.View.ToString()] = true;
			}
		}

		#endregion

	}
}
